#define  _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"

void QueueInit(Queue* pQ) {
	assert(pQ);
	pQ->head = NULL;
	pQ->tail = NULL;
	pQ->size = 0;
}

void QueueDestroy(Queue* pQ) {
	assert(pQ);
	QNode* cur = pQ->head;
	while (cur != NULL) {
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pQ->head = NULL;
	pQ->tail = NULL;
	pQ->size = 0;
}

void QueuePush(Queue* pQ, QDatatype x) {
	QNode* newnode = (QNode*)malloc(sizeof(QNode));

	newnode->next = NULL;
	newnode->data = x;

	if (pQ->head == NULL) {
		pQ->head = newnode;
		pQ->tail = newnode;
		pQ->size++;
	}
	else {
		pQ->tail->next = newnode;
		pQ->tail = newnode;
		pQ->size++;
	}
}

void QueuePop(Queue* pQ) {
	assert(pQ);
	if (pQ->head == NULL) {
		return;
	}
	if (pQ->head == pQ->tail) {
		free(pQ->head);
		pQ->head = NULL;
		pQ->tail = NULL;
		pQ->size = 0;
		return;
	}
	QNode* cur = pQ->head;
	pQ->head = pQ->head->next;
	pQ->size--;
	free(cur);
	cur = NULL;
}

int QueueSize(Queue* pQ) {
	return pQ->size;
}

bool QueueEmpty(Queue* pQ) {
	return pQ->size == 0;
}

QDatatype QueueFront(Queue* pQ) {
	if (!QueueEmpty(pQ)) {
		return pQ->head->data;
	}
	else {
		return -1;
	}
}

QDatatype QueueBack(Queue* pQ) {
	if (!QueueEmpty(pQ)) {
		return pQ->tail->data;
	}
	else {
		return -1;
	}
}